package com.spirng.leetcode.day02;

import org.w3c.dom.ls.LSInput;

public class GetIntersectionNode {
    public static void main(String[] args) {

    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int sizeA = 0;
        int sizeB = 0;
        ListNode curA = headA;
        ListNode curB = headB;
        while (curA != null) {
            curA = curA.next;
            sizeA++;
        }
        while (curB != null) {
            curB = curB.next;
            sizeB++;
        }
        if(sizeA==0||sizeB==0){
            return null;
        }
        int star;
        if (sizeA > sizeB) {
            star = sizeA - sizeB;
            curA=headA;
            curB=headB;
        } else {
            star = sizeB - sizeA;
            curA=headB;
            curB=headA;
        }
        for (int i = 0; i < star; i++) {
             curA = curA.next;
        }
        while (curA !=curB ) {
            curA = curA.next;
            curB = curB.next;
        }
        if (curA == null) {
            return null;
        }
        return curA;
    }
}
